package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.service.config.FeedAllocConfigUserService;
import com.qkl.xiatang.app.service.impl.ConfigService;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @Autowired
    private FeedAllocConfigUserService feedAllocConfigUserService;


    /**
     * 配置查料时间
     * @param params
     * @return
     */
    @PostMapping("/configCheckFeedTime")
    public Result configCheckFeedTime(@RequestBody Map<String,Object> params) {
        //Integer checkTime = StringUtils.trimToEmpty((String) params.get("checkTime"));
        String checkTime =  (String) params.get("checkTime");
        Integer iCheckTime = AppUtils.parseInt(checkTime);
        List<String> groupIdList  = (List<String>) params.get("groupIdList");
        groupIdList = AppUtils.stringsDistinct(groupIdList);
        if(null == checkTime || ObjectUtils.isEmpty(groupIdList)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        if(iCheckTime < 1){
            return Result.error(ErrorCodeUtils.PARAM_ILLEGAL, ErrorCodeUtils.PARAM_ILLEGAL_MSG);
        }
        return ControllerHelper.toOperateResult(configService.configCheckFeedTime(iCheckTime,groupIdList));
    }


    /**
     * 查询工作日志表头
     * @return
     */
    @GetMapping("/queryJobLogTitle")
    public Result queryJobLogTitle() {
        Map<String,Object> map = new HashMap<>();
        map.put("list",configService.queryJobLogTitle());
        return Result.success(map,"查询工作日志表头");
    }

    /**
     * 查询饲料喂料配置
     * @param params
     * @return
     */
    @GetMapping("/queryFeedAllocConfig")
    public Result queryFeedAllocConfig(@RequestParam Map<String,String> params) {
        String time = params.get("time");
        return ControllerHelper.toQueryResult(feedAllocConfigUserService.queryFeedAllocConfig(time));
    }

}
